﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GlobalVariables;
using StateGrid.ZWQK;

public partial class AddSYML : System.Web.UI.Page
{
    /// <summary>
    /// 中文期刊操作类
    /// </summary>
    private ZWQK_DAO zwqkDAO = new ZWQK_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.BindData();
        }
    }

    /// <summary>
    /// 绑定数据
    /// </summary>
    private void BindData()
    {
        //获取分类编码参数
        this.flbmTextBox.Text = Request["flbm"].ToString();
        if (null == this.flbmTextBox.Text || "".Equals(this.flbmTextBox.Text.Trim()))
        {
            String message = "对不起，没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE+"?message="+message);
            return;
        }

        //获取分类编码
        ZWQK_FL zwqkFL = zwqkDAO.getZwqkFLByFlbm(this.flbmTextBox.Text.Trim());

        if (null == zwqkFL)
        {
            String message = "对不起，没有找到对应的分类编码";
            Response.Redirect(GlobalResource.ERROR_PAGE + "?message=" + message);
            return;
        }

        //分类名称
        this.flmcTextBox.Text = zwqkFL.flmc;
        //索引编码
        this.sybmTextBox.Text = zwqkFL.flbm;

        //索引编码隐藏文本框
        this.sybmHiddenField.Value = zwqkFL.flbm;

        //中文名称
        this.zwmcTextBox.Text = "";
        //外文名称
        this.wwmcTextBox.Text = "";
        //主办单位
        this.zbdwTextBox.Text = "";
        //开放
        this.kfCheckBox.Checked = false;
        //创刊年份
        this.cknfTextBox.Text = "";
        //编辑单位
        this.bjdwTextBox.Text = "";
        //负责人
        this.fzrTextBox.Text = "";
        //通讯地址
        this.txdzTextBox.Text = "";
        //邮政编码
        this.yzbmTextBox.Text = "";
        //办公电话
        this.bgdhTextBox.Text = "";
        //传真电话
        this.czdhTextBox.Text = "";
        //电子邮件
        this.dzyjTextBox.Text ="";
        //发行周期
        //this.SetFXZQ(this.zwqk_syml.fxzq);
        this.fxzqDropDownList.SelectedIndex = 0;
        //国际刊号
        this.gjkhTextBox.Text = "";
        //国内刊号
        this.gnkhTextBox.Text = "";
        //资料来源
        this.zllyTextBox.Text = "" ;
        //邮发代号
        this.yfdhTextBox.Text = "";
        //简介
        this.jjTextBox.Text = "";
    }

    /// <summary>
    /// 提交
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (!this.SYBMValidate())
        {
            //Response.Write("<Script>alert('添加期刊索引失败')</Script>");
            return;
        }

        if (!this.ZWMCValidate())
        {
            return;
        }
       

        ZWQK_SYML zwqkSYML = this.GetZWQK_SYML();
        //添加索引编码
        this.zwqkDAO.insertZwqkSYML(zwqkSYML);

        this.BindData();
        Response.Write("<Script>alert('添加中文期刊索引信息成功')</Script>");

        //this.Response.Redirect(GlobalChineseJournal.LISTCHINESEJOURNALCATALOG_PAGE + "?currentCatalog=" + this.sybmHiddenField.Value);

    }
    /// <summary>
    /// 改变索引编码事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void sybmTextBox_TextChanged(object sender, EventArgs e)
    {
        this.SYBMValidate();
    }

   

    /// <summary>
    /// 获取页面中的信息
    /// </summary>
    /// <returns>一个索引目录对象</returns>
    private ZWQK_SYML GetZWQK_SYML()
    {
        ZWQK_SYML zwqkSYML = new ZWQK_SYML();
        
        //分类编码
        zwqkSYML.flbm = this.flbmTextBox.Text.Trim();
        //索引编码
        zwqkSYML.sybm = this.sybmTextBox.Text.Trim();
        //中文名称
        zwqkSYML.zwmc = this.zwmcTextBox.Text.Trim();
        //外文名称
        zwqkSYML.wwmc = this.wwmcTextBox.Text.Trim();
        //开放
        if (this.kfCheckBox.Checked)
        {
            zwqkSYML.kf = true;
        }
        else
        {
            zwqkSYML.kf = false;
        }

        //收录年份
        zwqkSYML.slnf = this.slnfTextBox.Text.Trim();

        //最新年份
        try
        {
            zwqkSYML.zxnf = Int32.Parse(this.zxnfTextBox.Text.Trim());
        }
        catch (Exception)
        {
        }

        //年份区间
        try
        {
            zwqkSYML.nfqj = Int32.Parse(this.nfqjTextBox.Text.Trim());
        }
        catch (Exception)
        {
        }
        //主办单位
        zwqkSYML.zbdw=this.zbdwTextBox.Text.Trim();
        //编辑单位
        zwqkSYML.bjdw = this.bjdwTextBox.Text.Trim();
        //通讯地址
        zwqkSYML.txdz = this.txdzTextBox.Text.Trim();
        //办公电话
        zwqkSYML.bgdh = this.bgdhTextBox.Text.Trim();
        //传真电话
        zwqkSYML.czdh = this.czdhTextBox.Text.Trim();
        //电子邮件
        zwqkSYML.dzyj = this.dzyjTextBox.Text.Trim();
        //创刊年份
        zwqkSYML.cknf = this.cknfTextBox.Text.Trim();
        //负责人
        zwqkSYML.fzr = this.fzrTextBox.Text.Trim();
        //邮政编码
        zwqkSYML.yzbm = this.yzbmTextBox.Text.Trim();
        //发行周期
        zwqkSYML.fxzq = this.fxzqDropDownList.SelectedValue;
        //国际刊号
        zwqkSYML.gjkh = this.gjkhTextBox.Text.Trim();
        //国内刊号
        zwqkSYML.gnkh = this.gnkhTextBox.Text.Trim();
        //邮发代号
        zwqkSYML.yfdh = this.yfdhTextBox.Text.Trim();
        //资料来源
        zwqkSYML.zlly = this.zllyTextBox.Text.Trim();
        //简介
        zwqkSYML.jj = this.jjTextBox.Text;

        return zwqkSYML;
    }

    /// <summary>
    /// 重新绑定数据
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void resetButton_Click(object sender, EventArgs e)
    {
        this.BindData();
    }

    /// <summary>
    /// 验证索引编码
    /// </summary>
    /// <returns></returns>
    private Boolean SYBMValidate()
    {
        Boolean flag = true;
        //清空索引编码信息
        this.errorLabel.Text = "";

        this.sybmTextBox.Text = this.sybmTextBox.Text.Trim();
        this.sybmHiddenField.Value = this.sybmHiddenField.Value.Trim();

        if (null == this.sybmTextBox.Text || this.sybmTextBox.Text.Length != (this.sybmHiddenField.Value.Length + 2))
        {
            this.errorLabel.Text = "错误：索引编码位数不正确，正确位数为" + (this.sybmHiddenField.Value.Length + 2) + "位";
            flag = false;
        }
        else if (!this.sybmTextBox.Text.StartsWith(this.sybmHiddenField.Value))
        {
            this.errorLabel.Text = "错误：索引编码前" + this.sybmHiddenField.Value.Length + "位不正确，应为：" + this.sybmHiddenField.Value;
            flag = false;
        }
        else
        {
            ZWQK_SYML zwqkSYML = zwqkDAO.getZwqkSYMLBySybm(this.sybmTextBox.Text);
            if (null != zwqkSYML)
            {
                this.errorLabel.Text = "错误：该分类编码已存在";
                flag = false;
            }
        }
        return flag;
    }

    /// <summary>
    /// 验证中文名称
    /// </summary>
    /// <returns></returns>
    private Boolean ZWMCValidate()
    {
        Boolean flag = true;
        //清空中文名称错误信息
        this.errorLabel.Text = "";
        if (null == this.zwmcTextBox.Text || "".Equals(this.zwmcTextBox.Text.Trim()))
        {
            this.errorLabel.Text = "错误:中文名称不能为空";
            //Response.Write("<Script>alert('添加期刊索引失败')</Script>");
            flag = false;
        }
        return flag;
    }
}